package com.qfjg.controllers;

import com.qfjg.entity.DumpRecord;
import com.qfjg.entity.DumpRecordQueryInfo;
import com.qfjg.myutil.ResultData;
import com.qfjg.service.DumpRecordService;
import com.qfjg.webcore.common.Utils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "DumpRecord", description = "倾废信息相关")
@RestController
@RequestMapping("/${spring.application.name}/dumpRecord")
public class DumpRecordController {

    @Autowired
    DumpRecordService dumpRecordService;


    @PostMapping("/insertData")
    @Operation(summary = "插入数据")
    public ResultData insert(@RequestBody DumpRecord dumpRecord) {

        try {
            dumpRecord.setDumpId(Utils.uuid());
            dumpRecordService.insert(dumpRecord);
            return ResultData.SUCCESS();
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultData<>(444, "出现错误: " + e.getMessage(), null);
        }
    }

    @PostMapping("/updateData")
    @Operation(summary = "通过dumpId修改数据")
    public ResultData updateById(@RequestBody DumpRecord dumpRecord) {
        try {
            boolean b = dumpRecordService.updateById(dumpRecord);
            if (b) {
                return ResultData.SUCCESS();
            }
            return new ResultData<>(444, "出现错误: 更新操作失败", null);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultData<>(444, "出现错误: " + e.getMessage(), null);
        }
    }


    @PostMapping("/deleteData/{dumpId}")
    @Operation(summary = "通过dumpId删除数据")
    public ResultData deleteById(@PathVariable String dumpId) {
        try {
            boolean b = dumpRecordService.deleteById(dumpId);
            if (b) {
                return ResultData.SUCCESS();
            }
            return new ResultData<>(444, "出现错误: 删除操作失败", null);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultData<>(444, "出现错误: " + e.getMessage(), null);
        }
    }

    @PostMapping("/selectDumpRecordList/{projectId}")
    @Operation(summary = "通过projectId查询倾废记录数据")
    public ResultData selectDumpRecordList(@PathVariable String projectId) {
        try {
            List<DumpRecord> dumpRecords = dumpRecordService.selectDumpRecordList(projectId);
            return new ResultData<>(dumpRecords);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultData<>(444, "出现错误: " + e.getMessage(), null);
        }
    }

    @PostMapping("/selectDumpRecordListByQuery")
    @Operation(summary = "通过projectId查询倾废记录数据")
    public ResultData selectDumpRecordListByQuery(@RequestBody DumpRecordQueryInfo dumpRecordQueryInfo) {
        try {
            List<DumpRecord> dumpRecords = dumpRecordService.selectDumpRecordListByQuery(dumpRecordQueryInfo);
            return new ResultData<>(dumpRecords);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultData<>(444, "出现错误: " + e.getMessage(), null);
        }
    }

}
